﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.BaseWebCtrlLib;
using HF.Framework.DataClientDBAgent;
using HF.Framework.SqlDataContract;


    public partial class Qingjia : BaseUserControl//继承表单基类
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load();//继承基类的方法，可以控制表单中控件的enbale属性和readonly属性
            if (!IsPostBack)
                InitData();

        }

        /// <summary>
        /// 表单数据展现，考虑表单退回重新处理和保存草稿
        /// 的情况
        /// </summary>
        private void InitData()
        {
            string sql = "select * from testQingjia where workflowinsId=@workflowinsId";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = sql;
            sqlItem.AppendParameter("@workflowinsId", WorkflowInsId);
            ClientDBAgent agent = new ClientDBAgent();
            DataTable dt = agent.ExecuteDataTable(sqlItem);

            if (dt != null && dt.Rows.Count > 0)//判断是否有数据，有数据读取数据库中的值
            {
                lbUserId.Text = dt.Rows[0]["userid"].ToString();
                lbUserName.Text = dt.Rows[0]["userName"].ToString();
                lbDutyCaption.Text = dt.Rows[0]["dutyCaption"].ToString();
                lbArchCaption.Text = dt.Rows[0]["archCaption"].ToString();
                tbxStartTime.Text = dt.Rows[0]["beginTime"].ToString();
                tbxEndTime.Text = dt.Rows[0]["endTime"].ToString();
                tbxDays.Text = dt.Rows[0]["Days"].ToString();
                tbxQingjia.Text = dt.Rows[0]["QingJia"].ToString();
                dplType.Text = dt.Rows[0]["QingJiaType"].ToString();
            }
            else//如果没有数据，初始化默认值
            {
                lbUserId.Text = UserId;
                lbUserName.Text = UserName;
                lbDutyCaption.Text = DutyCaption;
                lbArchCaption.Text = ArchCaption;
                tbxStartTime.Text = DateTime.Now.ToShortDateString();
                tbxEndTime.Text = DateTime.Now.ToShortDateString();
            }
        }
        /// <summary>
        /// 表单数据提交，避免重复提交
        /// </summary>
        public override void SaveUserControl(bool IsDraft)
        {
            base.SaveUserControl(IsDraft);
            SqlDataItem sqlItem1 = new SqlDataItem();
            SqlDataItem sqlItem2 = new SqlDataItem();
            string sql = "delete testQingjia where workflowinsId=@WorkflowInsId";//先删除原有数据
            sqlItem1.CommandText = sql;
            sqlItem1.AppendParameter("@WorkflowInsId", WorkflowInsId);

            sql = "insert testQingjia(userId,username,dutycaption,archcaption,begintime,endtime,days,qingjia,qingjiatype," +
            "workflowId,workflowInsId,worktaskId,worktaskInsId)" +
                " values(@userId,@username,@dutycaption,@archcaption,@begintime,@endtime,@days,@qingjia,@qingjiatype," +
                "@workflowId,@workflowInsId,@worktaskId,@worktaskInsId)";
            sqlItem2.CommandText = sql;
            sqlItem2.AppendParameter("@userId", lbUserId.Text);
            sqlItem2.AppendParameter("@username", lbUserName.Text);
            sqlItem2.AppendParameter("@dutycaption", lbDutyCaption.Text);
            sqlItem2.AppendParameter("@archcaption", lbArchCaption.Text);
            sqlItem2.AppendParameter("@begintime", tbxStartTime.Text);
            sqlItem2.AppendParameter("@endtime", tbxEndTime.Text);
            sqlItem2.AppendParameter("@days", tbxDays.Text);
            sqlItem2.AppendParameter("@qingjia", tbxQingjia.Text);
            sqlItem2.AppendParameter("@qingjiatype", dplType.SelectedItem.Text);
            sqlItem2.AppendParameter("@workflowId", WorkflowId);
            sqlItem2.AppendParameter("@workflowInsId", WorkflowInsId);
            sqlItem2.AppendParameter("@worktaskId", WorktaskId);
            sqlItem2.AppendParameter("@worktaskInsId", WorktaskInsId);


            SqlDataItemArray sqlItemAry = new SqlDataItemArray();
            sqlItemAry.AppendSqlItem(sqlItem1);
            sqlItemAry.AppendSqlItem(sqlItem2);

            ClientDBAgent agent = new ClientDBAgent();
            agent.ExecuteNonQueryArray(sqlItemAry);

        }
       
}

